#!/usr/bin/python
# -*- coding: UTF-8 -*-



from django.shortcuts import render
from django.http import HttpResponseRedirect, JsonResponse

from loginControllerClass import *
from models import *
from django.template import RequestContext
# Create your views here.
import time
from models import workrecord

# 登录
def login(request):
    c_login = LoginControllerClass(request)
    return c_login.login()
# 退出
def logout(request):
    c_login = LoginControllerClass(request)
    return c_login.logout()

# 首页
def work(request):
    c_login = LoginControllerClass(request)
    return c_login.index()

def geterror(request):
    systemname=request.POST.get('system')

    sysid=system.objects.values('id').get(systemname=systemname)
    print sysid

    data = error.objects.filter(system_id=sysid['id']).values('errortype')
    print list(data)
    return JsonResponse(list(data),safe=False)

def addrecord(request):
    print request.body
    workername = request.session.get('user')
    shopname = request.POST.get('shopselect')
    systemname = request.POST.get('systemselect')
    errortype = request.POST.get('errorselect')

    if shopname =='' or systemname=='' or errortype=='':
        return JsonResponse(False, safe=False)
    flowtime =time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
    dic = {'worker': workername, 'shopname': shopname,'systemname':systemname,'errortype':errortype,'flowtime':flowtime}
    print record.objects.create(**dic)
    data=record.objects.all().filter(worker=workername).order_by('-id').values()
    print list(data)
    return JsonResponse({'records':list(data)},safe=False)

def addtr(request):
    print request.body
    workername = request.session.get('user')
    type = request.POST.get('type')
    comment = request.POST.get('comment')
    recordtime = request.POST.get('time')
    mark = request.POST.get('mark')

    if type == '' or comment == '' or recordtime == '':
        return JsonResponse(False, safe=False)


    flowtime ='%s  %s' %(time.strftime('%Y-%m-%d',time.localtime(time.time())),recordtime)
    dic = {'worker': workername, 'type': type, 'comment':comment,'mark': mark,
           'time': flowtime}
    print dic
    print workrecord.objects.create(**dic)
    data = workrecord.objects.all().filter(worker=workername).order_by('-id').values()
    print list(data)
    return JsonResponse({'workrecords': list(data)}, safe=False)

def deletetr(request):
    print request.body
    id = request.POST.get('id')
    workername = request.session.get('user')

    if id =='':
        return JsonResponse(False, safe=False)
    try:
        workrecord.objects.get(id=id).delete()

        data=workrecord.objects.all().filter(worker=workername).order_by('-id').values()
        return JsonResponse({'workrecord':list(data)},safe=False)
    except Exception,e:
        return JsonResponse(False, safe=False)


def deleterecord(request):
    print request.body
    id = request.POST.get('id')
    workername = request.session.get('user')

    if id =='':
        return JsonResponse(False, safe=False)
    try:
        record.objects.get(id=id).delete()

        data=record.objects.all().filter(worker=workername).order_by('-id').values()
        return JsonResponse({'records':list(data)},safe=False)
    except Exception,e:
        return JsonResponse(False, safe=False)
